import router from '@/router'
import store from '@/store'

const whitePage = ['/login', '/register', '/404']

router.beforeEach(async(to, from, next) => {
  store.commit('app/SET_CRUMBS', [{ path: to.path, title: to.meta.title }])
  if (store.state.user.token) {
    if (to.path.toLowerCase() === '/login') {
      next('/')
      return
    }
    if (!store.state.user.userInfo.id) {
      // await store.dispatch('user/getUserInfo')
    }
    next()
  } else {
    if (whitePage.includes(to.path.toLowerCase())) {
      next()
      return
    }
    next('/login?redirect=' + to.fullPath)
  }
})
